package com.foxhis.webstart.gui;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import javax.swing.JApplet;

import netscape.javascript.JSObject;

public class InitMain extends JApplet {
	public String serverxml;

	public InitMain() {

	}

	public String getProgramurl() {
		Process pp;
		String result = System.getProperty("user.home") + "/Program Files";
		try {
			pp = Runtime.getRuntime().exec("cmd /c echo %programfiles%");
			InputStream in = pp.getInputStream();
			String tem = "";
			BufferedReader rd = new BufferedReader(new InputStreamReader(in));
			if ((tem = rd.readLine()) != null) {
				result = tem;
			}
			rd.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			// updateText(e.getMessage());
		}
		return result;
	}

	public void init() {
		setVisible(true);
		serverxml = actionJS("getServerXml()");
		String split[] = serverxml.split(";");
		String systemname = "Pms";
		String downloadpath = getProgramurl();
		for (String temp : split) {
			if (temp.startsWith("systemname")) {
				systemname = getSpliValue(temp.split("="), systemname);
				if (systemname.isEmpty()) {
					systemname = "Pms";
				}
			} else if (temp.startsWith("download-path")) {
				downloadpath = getSpliValue(temp.split("="), downloadpath);
				if (downloadpath.isEmpty()) {
					downloadpath = getProgramurl();
				}
			}
		}
		String homeurl = downloadpath + "/" + systemname + "/";
		File historyfile = new File(homeurl + systemname.toLowerCase() + ".ini");
		Properties base = new Properties();
		InputStream is = null;
		String modelxml = actionJS("getModelXml()");
		String result = "0";
		if (historyfile.exists()) {
			try {
				is = new FileInputStream(historyfile);
				base.load(is);
				is.close();
				String modelstr = base.getProperty("Model");
				if (modelstr != null) {
					if (modelstr.matches("[0-9]*")) {
						result = modelstr;
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		boolean hasset = false;
		if (modelxml != null) {
			String[] tt = modelxml.split(",");
			for (String temp : tt) {
				if (temp.contains("/")) {
					String tresult = temp.substring(0, temp.indexOf("/"));
					if (tresult.equals(result)) {
						hasset = true;
						break;
					}
				}
			}
		}
		if (!modelxml.isEmpty() && !hasset) {
			if (modelxml.contains("/")) {
				result = modelxml.substring(0, modelxml.indexOf("/"));
			}
		}
		System.out.println("HistoryModel:" + result);
		actionJS("setMainModel(" + result + ")");
	}

	public String actionJS(String name) {
		String s = null;
		try {
			JSObject win = JSObject.getWindow(this);
			Object result = win.eval(name);
			s = result + "";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return s;
	}

	private String getSpliValue(String[] split, String defaultvalue) {
		if (split.length > 1) {
			return split[1];
		} else {
			return "";
		}
	}

}
